package com.testingedu.listener;

import java.sql.Connection;
import java.sql.SQLException;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import com.testingedu.db.DBManager;
/**
 * 数据源操作类，当tomcat启动的时候初始化数据源，而当tomcat关闭的时候则负责关闭数据源
 * @author lsww
 * @E-mail lsww90@Gmail.com
 * @createtime 2013-1-29 下午04:36:32
 * @version 1.0
 * @since JDK 1.6
 */
public class MyServletContextListener implements ServletContextListener {

	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		DBManager.closeDataSource();
		System.out.println("数据源关闭成功！");
	}

	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		Connection conn = DBManager.getConnection();
		if (conn != null) {
			System.out.println("连接池初始化成功！");
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} else {
			System.out.println("初始化连接池失败，获取了一个空连接");
		}
	}

}